#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+5,M=20;
int t,n,m,k,a[N],c[N];
int d[N],x[5],isp[N];
int b[M][N],g[M][N];
int get(int k){
    for(int i=1;i<=(1<<k);++i)
        g[0][i]=i;
    for(int i=1;i<=k;++i){
        for(int j=1;j<=(1<<k-i);++j){
            if(!b[i][j]){
                if(d[g[i-1][j*2-1]]>=i)
                    g[i][j]=g[i-1][j*2-1];
                else g[i][j]=g[i-1][j*2];
            }else{
                if(d[g[i-1][j*2]]>=i)
                    g[i][j]=g[i-1][j*2];
                else g[i][j]=g[i-1][j*2-1];
            }
        }
    }return g[k][1];
}
signed main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;++i)
        cin>>a[i];
    for(int i=1;i<=m;++i)
        cin>>c[i];
    k=0;
    while((1<<k)<n)++k;
    for(int i=1;i<=k;++i)
        for(int j=1;j<=(1<<k-i);++j){
            char ch;cin>>ch;
            b[i][j]=ch-'0';
        }
    cin>>t;
    while(t--){
        cin>>x[0]>>x[1]>>x[2]>>x[3];
        for(int i=1;i<=n;++i)
            a[i]^=x[i%4];
        int ans=0;
        for(int i=1;i<=m;++i){
            k=0;
            while((1<<k)<c[i])++k;
            for(int j=1;j<=c[i];++j)d[j]=a[j];
            for(int j=c[i]+1;j<=(1<<k)+1;++j){
                d[j]=(1ll<<31ll)-1;
                isp[get(k)]=1;
                d[j]=0;
            }int res=0;
            for(int j=1;j<=(1<<k);++j){
                res+=isp[j]*j,isp[j]=d[j]=0;
            }
            ans^=(res*i);
        }cout<<ans<<'\n';
        for(int i=1;i<=n;++i)
            a[i]^=x[i%4];
    }
    return 0;
}